import 'package:enjoy_plus_bryant/pages/house/building_list.dart';
import 'package:enjoy_plus_bryant/pages/house/house_detail.dart';
import 'package:enjoy_plus_bryant/pages/house/house_form.dart';
import 'package:enjoy_plus_bryant/pages/house/house_list.dart';
import 'package:enjoy_plus_bryant/pages/house/location_list.dart';
import 'package:enjoy_plus_bryant/pages/house/room_list.dart';
import 'package:enjoy_plus_bryant/pages/login/index.dart';
import 'package:enjoy_plus_bryant/pages/notice/index.dart';
import 'package:enjoy_plus_bryant/pages/profile/index.dart';
import 'package:enjoy_plus_bryant/pages/tab_bar/index.dart';
import 'package:enjoy_plus_bryant/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      // 这里配置只要点击就能跳转
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => NoticeDetail(),
        '/login': (context) => LoginPage(),
      },
      // 获取用户跳转页面的路径
      // 如果有token就放行
      onGenerateRoute: (settings) {
        // 1. 从仓库中获取token
        final token = TokenManager().getToken();
        // 2. 判断token是否为空且去的路径是否为登录页面，若为空且去的不是登录页面，则返回登录页面
        if (token == null && settings.name != '/login') {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }
        // 3. 登录后才可访问 - 个人信息页 -  否则返回去设置的页面
        if (settings.name == '/profile') {
          // print(settings.arguments);
          return MaterialPageRoute(
            builder: (context) =>
                ProfilePage(userInfo: settings.arguments as Map),
          );
        }
        // 4.登录后才可访问 - 房屋列表页 -  否则返回去设置的页面
        if (settings.name == '/house_list') {
          // print(settings.arguments);
          return MaterialPageRoute(builder: (context) => HouseList());
        }
        // 5.登录后才可访问 - 增加房屋页 -  否则返回去设置的页面
        if (settings.name == '/add_house') {
          // print(settings.arguments);
          return MaterialPageRoute(builder: (context) => LocationList());
        }
        // 6.登录后才可访问 - 楼栋列表页 -  否则返回去设置的页面
        if (settings.name == '/building_list') {
          // print(settings.arguments);
          return MaterialPageRoute(
            builder: (context) =>
                BuildingList(point: (settings.arguments as Map)['point']),
          );
        }
        // 7.登录后才可访问 - 房间列表页 -  否则返回去设置的页面
        if (settings.name == '/room_list') {
          Map data = settings.arguments as Map;
          // print(settings.arguments);
          return MaterialPageRoute(
            builder: (context) =>
                RoomList(point: data['point'], building: data['building']),
          );
        }
        // 8.登录后才可访问 - 房屋信息页 -  否则返回去设置的页面
        if (settings.name == '/house_form') {
          Map data = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => HouseForm(houseData: data, id: data['id']),
          );
        }

        // 9.登录后才可访问 - 房屋详情页 -  否则返回去设置的页面
        if (settings.name == '/house_detail') {
          Map data = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => HouseDetail(id: data['id']),
          );
        }

        // 没有匹配到匹配的页面路径，则返回首页
        return MaterialPageRoute(builder: (context) => TabBarPage());
      },
      initialRoute: '/',
    ),
  );
}
